﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Net.Sockets;
using System.Net;

namespace WinShare
{
    public partial class ShareSiteForm : Form
    {
        private bool anySite = false;
        public MessageAPI.UdpMessage WebSite;
        public ShareSiteForm()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            errorProvider1.Clear();
            if (txtWebSite.Text == "")
            {
                errorProvider1.SetError(txtWebSite, "Please Enter a Website");
            }
            else if (cbxRecipient.Text == "")
            {
                errorProvider1.SetError(cbxRecipient, "Please enter a Recipient");
            }
            else
            {
                bool goodSite = false;
                if (anySite == false)
                {
                    goodSite = CheckURL(txtWebSite.Text);
                }
                else
                {
                    goodSite = true;
                }

                if (goodSite == true)
                {
                    WebSite = new MessageAPI.UdpMessage();
                    WebSite.Message = txtWebSite.Text;
                    WebSite.IntendedUser = new MessageAPI.UdpUser(cbxRecipient.Text);
                    WebSite.MessageType = MessageAPI.UdpMessage.udpMessageType.WebsiteShare;

                    WinShare.Properties.Settings.Default.User_History.Add(cbxRecipient.Text);
                    WinShare.Properties.Settings.Default.Save();

                    this.DialogResult = System.Windows.Forms.DialogResult.OK;
                    this.Close();
                }
                else
                {
                    errorProvider1.SetError(txtWebSite, "WebSite not Valid");
                }
            }
        }

        private bool CheckURL(string url)
        {
            label3.Text = "Validating Website. Please wait..";
            if (url.Substring(0, 7) != "http://" && url.Substring(0, 8) != "https://")
            {
                url = "http://" + url;
            }
            bool br = false;
            try
            {
                System.Uri mainURL = new System.Uri(url);
                IPHostEntry ipHost = Dns.GetHostEntry(mainURL.DnsSafeHost);
                br = true;
                label3.Text = "Website is valid.";
            }
            catch (SocketException se)
            {
                br = false;
                label3.Text = "Webiste is invalid";
            }
            catch (UriFormatException uex)
            {
                br = false;
                label3.Text = "Webiste is invalid";
            }
            return br;
        }

        private void ShareSiteForm_Shown(object sender, EventArgs e)
        {
            txtWebSite.Focus();
        }

        private void ShareSiteForm_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Control == true && e.Shift == true && e.KeyCode == Keys.A)
            {
                anySite = true;
            }
        }

        private void ShareSiteForm_Load(object sender, EventArgs e)
        {
            if (WinShare.Properties.Settings.Default.User_History != null)
            {
                foreach (string historyItem in WinShare.Properties.Settings.Default.User_History)
                {
                    if (!cbxRecipient.Items.Contains(historyItem))
                    {
                        cbxRecipient.Items.Add(historyItem);
                    }
                }
            }
            else
            {
                WinShare.Properties.Settings.Default.User_History = new System.Collections.Specialized.StringCollection();
            }
        }

        private void ShareSiteForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (cbxRecipient.Items != null)
            {
                WinShare.Properties.Settings.Default.User_History.Clear();
                foreach (string historyItem in cbxRecipient.Items)
                {
                    if (!WinShare.Properties.Settings.Default.User_History.Contains(historyItem))
                    {
                        WinShare.Properties.Settings.Default.User_History.Add(historyItem);
                    }
                }
            }
        }
    }
}
